package org.example.controller;

import org.example.data.Result;
import org.example.data.User;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserController {

    public Result<User> login(String username, String password) {
        // 假设登录逻辑需要查询数据库，这里演示调用 getUser 方法
        try {
            User user = getUser(username);
            if (user != null && user.getPassword().equals(password)) {
                return new Result<>(true, "登录成功", user);
            } else {
                return new Result<>(false, "用户名或密码错误", null);
            }
        } catch (SQLException e) {
            System.out.println("数据库查询出错：" + e.getMessage());
            return new Result<>(false, "数据库查询出错", null);
        }
    }

    public Result<Integer> sign(String username, String password) {
        try {
            User user = getUser(username);
            if (user != null && user.getPassword().equals(password)) {
                return new Result<>(false, "账户已存在", 0);
            } else {
                Integer n = insertUser(username,password);
                if (n == 1){
                    return new Result<>(true,"注册成功",1);
                }
                return new Result<>(false,"注册成功",n);
            }
        } catch (SQLException e) {
            System.out.println("数据库查询出错：" + e.getMessage());
            return new Result<>(false, "数据库查询出错", 1);
        }
    }

    public Result<Integer> update(Integer id, String password,String passwordTwo) {
        try {
            if (password.equals(getPassword(id))){
                Integer n = updatePassword(id, passwordTwo);
                if (n == 1){
                    return new Result<>(true,"修改成功",1);
                }
                return new Result<>(false,"修改失败",n);
            }else {
                return new Result<>(false,"原密码错误",0);
            }
        } catch (SQLException e) {
            System.out.println("密码修改过程中出现错误：" + e.getMessage());
            return new Result<>(false, "修改密码失败", null);
        }
    }

    private User getUser(String username) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "select * from user where username = ?";
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1, username); // 设置查询参数
        ResultSet resultSet = statement.executeQuery(); // 执行查询

        User user = null;
        if (resultSet.next()) {
            int id = resultSet.getInt("id");
            String password = resultSet.getString("password");
            user = new User(id, username, password);
        }
        resultSet.close();
        statement.close();
        mySQL.close();
        return user;
    }

    private Integer insertUser(String username, String password) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "insert into user (username, password) values (?, ?)";
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1, username);
        statement.setString(2, password);
        int n = statement.executeUpdate(); // 执行插入操作
        statement.close();
        mySQL.close();

        return n;
    }
    private String getPassword(Integer id) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "select password from user where id = ?";
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setInt(1, id); // 设置查询参数
        ResultSet resultSet = statement.executeQuery(); // 执行查询

        String password = null;
        if (resultSet.next()) {
            password = resultSet.getString("password");
        }
        resultSet.close();
        statement.close();
        mySQL.close();
        return password;
    }

    private Integer updatePassword(Integer id, String password) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "update user set password = ? where id = ?";
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1, password);
        statement.setInt(2, id);
        int n = statement.executeUpdate(); // 执行更新操作
        statement.close();
        mySQL.close();
        return n;
    }
}